Django is a Python-based web framework used by millions of developers and billions of consumers through popular apps like Instagram.
A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.
Example:
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
Example:
from django.db import models
class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100)
class Album(models.Model):
artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
Field Types:
<input type="text">
, <select>
).the power of relational databases lies in relating tables to each other. Django offers ways to define the three most common types of database relationships: many-to-one, many-to-many and one-to-one.
Many-To-One-Relationship:
from django.db import models
class Manufacturer(models.Model):
# ...
pass
class Car(models.Model):
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
# ...
Many-to-many relationships:
Example:
from django.db import models
class Topping(models.Model):
# ...
pass ` `class Pizza(models.Model):`
# ...
toppings = models.ManyToManyField(Topping)
One-to-one relationships
OneToOneField requires a positional argument: the class to which the model is related.
There are three styles of inheritance that are possible in Django.
A clean, elegant URL scheme is an important detail in a high-quality Web application. Django lets you design URLs however you want, with no framework limitations.
How Django processes a request
Example:
from django.urls import path
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),`
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail), `]`
The following path converters are available by default:
Django defines a standard API for loading and rendering templates regardless of the backend. Loading consists of finding the template for a given identifier and preprocessing it.
A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags.
The syntax of the Django template language involves four constructs.
Comments look like this:
{# this won't be rendered #}
GET and POST
GET and POST are the only HTTP methods to use when dealing with forms.
Django’s login form is returned using the POST method, in which the browser bundles up the form data, encodes it for transmission, sends it to the server, and then receives back its response.
GET would also be unsuitable for a password form, because the password would appear in the URL, and thus, also in browser history and server logs, all in plain text.
Django handles three distinct parts of the work involved in forms:
When rendering an object in Django, we generally:
Example:
<form action="/your-name/" method="post">
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" value="">
<input type="submit" value="OK"> `</form>`
The Form class
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
The distinction between Bound and unbound forms is important:
The auth system consists of:
The admin is enabled in the default project template used by startproject.
class ModelAdmin
The ModelAdmin class is the representation of a model in the admin interface. Example:
from django.contrib import admin
from myapp.models import Author
class AuthorAdmin(admin.ModelAdmin):
pass `admin.site.register(Author, AuthorAdmin)`
from django.contrib import admin
from .models import Author
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
pass
The goal of internationalization and localization is to allow a single Web application to offer its content in languages and formats tailored to the audience.
Essentially, Django does two things:
internationalization
Preparing the software for localization. Usually done by developers.
localization
Writing the translations and local formats. Usually done by translators.
Django provides multiple protections against:
Almost all popular open source packages have some degree of funding involved, typically in one of three forms:
1) Corporate Sponsor - A group of engineers within a larger, for-profit company decide to open-source internal code.
2) Solo - An individual developer initially creates code, open sources it, and retains default control.
3) Non-profit - This was Django’s approach early on, in 2008, when the Django Software Foundation was formed to promote, support, and advance Django.
Django’s organization is managed by a non-profit, the DSF, with a miniscule budget. And Django code is lead by a core team of volunteers, two paid Django Fellows, and a larger group of contributors.